﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FriendStar.Code;
using FriendStar.Application.BOM;
using FriendStar.Application.MainData;
using FriendStar.Domain.Entity.MainData;

namespace FriendStar.Web.Areas.MainData.Controllers
{
    public class AccessoriesController : ControllerBase
    {
        //
        // GET: /MainData/Accessories/
        V_BUS_SJ_DictionaryApp v_bus_sj_dictionaryApp = new V_BUS_SJ_DictionaryApp();
        ZSJ_B1_GH_SupplierInformationApp zsj_b1_gh_supplierinformationApp = new ZSJ_B1_GH_SupplierInformationApp();
        V_ZSJ_31_FL_AccessoriesApp v_zsj_31_fl_accessoriesApp = new V_ZSJ_31_FL_AccessoriesApp();
        ZSJ_31_FL_AccessoriesApp zsj_31_fl_accessoriesApp = new ZSJ_31_FL_AccessoriesApp();
        V_ZSJ_32_GH_SuAccessoriesApp v_zsj_32_gh_suaccessoriesApp = new V_ZSJ_32_GH_SuAccessoriesApp();
        ZSJ_32_GH_SuAccessoriesApp zsj_32_gh_suaccessoriesApp = new ZSJ_32_GH_SuAccessoriesApp();
        V_ZSJ_32_GH_SuAccessoriesNApp v_zsj_32_gh_suaccessoriesnApp = new V_ZSJ_32_GH_SuAccessoriesNApp();

        [HttpGet]
        [HandlerAuthorize]
        public ActionResult AccessoriesList()
        {
            ViewBag.accessoriesType = v_bus_sj_dictionaryApp.GetListByClassNo("09");
            ViewBag.purpose = v_bus_sj_dictionaryApp.GetListByClassNo("10");
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetAccessoriesListJson(Pagination pagination, string Accessories, string SpeciModel, string AccessoriesTypeNO, string purposeNO)
        {
            var data = new
            {
                rows = v_zsj_31_fl_accessoriesApp.GetList(pagination, Accessories, SpeciModel, AccessoriesTypeNO, purposeNO),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetAccessoriesSortJson(string sidx, string sord, int rows, string Accessories, string SpeciModel, string AccessoriesTypeNO, string purposeNO, string currNumber)
        {
            Pagination pagination = new Pagination();
            pagination.rows = int.MaxValue;
            pagination.page = 1;
            pagination.sidx = sidx;
            pagination.sord = sord;
            var list = v_zsj_31_fl_accessoriesApp.GetList(pagination, Accessories, SpeciModel, AccessoriesTypeNO, purposeNO);
            int index = 0;
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].AccessoriesID == currNumber)
                {
                    index = i + 1;
                    break;
                }
            }
            if (index > 0)
            {
                var data = new
                {
                    currIndex = index % rows,
                    currPage = (index % rows == 0) ? (index / rows) : (index / rows) + 1
                };
                return Content(data.ToJson());
            }
            else
            {
                var data = new
                {
                    currIndex = 1,
                    currPage = 1
                };
                return Content(data.ToJson());
            }
        }
        [HttpGet]
        [HandlerAuthorize]
        public ActionResult AccessoriesListInput(string status, string number)
        {
            ViewBag.status = (status != "U") ? "I" : status;
            ViewBag.number = (number == null) ? "" : number;
            ViewBag.accessoriesType = v_bus_sj_dictionaryApp.GetListByClassNo("09");
            ViewBag.purpose = v_bus_sj_dictionaryApp.GetListByClassNo("10");
            ViewBag.meaunits = v_bus_sj_dictionaryApp.GetListByClassNo("06");
            if (status == "U")
            {
                ViewBag.oldEntity = v_zsj_31_fl_accessoriesApp.GetForm(number).ToJson();
            }
            return View();
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitAccessoriesListForm(string status, ZSJ_31_FL_AccessoriesEntity entity, string keyValue)
        {
            if (status == "I")
            {
                if (zsj_31_fl_accessoriesApp.CheckedEntity(entity))
                {
                    zsj_31_fl_accessoriesApp.SubmitForm(entity, "");
                    return Success("新增操作成功。", entity.AccessoriesID);
                }
                else
                {
                    return Error("新增失败，已经存在相同零件编号数据。");
                }
            }
            else if (status == "U")
            {
                if (zsj_31_fl_accessoriesApp.CheckedEntity(entity))
                {
                    ZSJ_31_FL_AccessoriesEntity oldEntity = zsj_31_fl_accessoriesApp.GetForm(entity.AccessoriesID);
                    if (oldEntity != null)
                    {
                        zsj_31_fl_accessoriesApp.SubmitForm(entity, entity.AccessoriesID);
                        return Success("修改操作成功。");
                    }
                    else
                    {
                        return Error("修改失败，没找到待修改的容器数据。");
                    }
                }
                else
                {
                    return Error("修改失败，已经存在相同容器编号数据。");
                }
            }
            else if (status == "D")
            {

                ZSJ_31_FL_AccessoriesEntity oldEntity = zsj_31_fl_accessoriesApp.GetForm(keyValue);
                if (oldEntity != null)
                {
                    zsj_31_fl_accessoriesApp.DeleteForm(oldEntity.AccessoriesID);

                    return Success("删除操作成功。");
                }
                else
                {
                    return Error("删除失败，没找到待删除的容器数据。");
                }
            }
            else
            {
                return Error("不可识别的操作类型。");
            }
        }

        [HttpGet]
        [HandlerAuthorize]
        public ActionResult SupplierList()
        {
            ViewBag.accessoriesType = v_bus_sj_dictionaryApp.GetListByClassNo("09");
            ViewBag.proCompany = zsj_b1_gh_supplierinformationApp.GetList("IsAccessories ='1'");
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSupplierListJson(Pagination pagination, string Accessories, string FProCompanyNO, string AccessoriesTypeNO)
        {
            var data = new
            {
                rows = v_zsj_32_gh_suaccessoriesApp.GetList(pagination, Accessories, FProCompanyNO, AccessoriesTypeNO),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSupplierSortJson(string sidx, string sord, int rows, string Accessories, string FProCompanyNO, string AccessoriesTypeNO, string currNumber)
        {
            Pagination pagination = new Pagination();
            pagination.rows = int.MaxValue;
            pagination.page = 1;
            pagination.sidx = sidx;
            pagination.sord = sord;
            var list = v_zsj_32_gh_suaccessoriesApp.GetList(pagination, Accessories, FProCompanyNO, AccessoriesTypeNO);
            int index = 0;
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].AccessoriesID == currNumber)
                {
                    index = i + 1;
                    break;
                }
            }
            if (index > 0)
            {
                var data = new
                {
                    currIndex = index % rows,
                    currPage = (index % rows == 0) ? (index / rows) : (index / rows) + 1
                };
                return Content(data.ToJson());
            }
            else
            {
                var data = new
                {
                    currIndex = 1,
                    currPage = 1
                };
                return Content(data.ToJson());
            }
        }
        [HttpGet]
        [HandlerAuthorize]
        public ActionResult GetSupplierNListJson(Pagination pagination, string Accessories)
        {
            var data = new
            {
                rows = v_zsj_32_gh_suaccessoriesnApp.GetList(pagination, Accessories),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }
        [HttpGet]
        [HandlerAuthorize]
        public ActionResult SupplierListInput(string status, string number, string type)
        {
            ViewBag.status = (status != "U") ? "I" : status;
            ViewBag.number = (number == null) ? "" : number;
            ViewBag.type = (type != "1") ? "0" : type;
            ViewBag.accessoriesType = v_bus_sj_dictionaryApp.GetListByClassNo("09");
            ViewBag.proCompany = zsj_b1_gh_supplierinformationApp.GetList("IsAccessories ='1'");
            ViewBag.oldEntity = v_zsj_32_gh_suaccessoriesApp.GetForm(number).ToJson();
            return View();
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitSupplierListForm(string status, ZSJ_32_GH_SuAccessoriesEntity entity, string keyValue)
        {
            if (status == "I")
            {
                entity.AccessoriesID = "";
                if (zsj_32_gh_suaccessoriesApp.CheckedEntity(entity))
                {
                    zsj_32_gh_suaccessoriesApp.SubmitForm(entity, "");
                    return Success("新增操作成功。", entity.AccessoriesID);
                }
                else
                {
                    return Error("新增失败，已经存在相同供应商和辅料编号数据。");
                }
            }
            else if (status == "U")
            {
                if (zsj_32_gh_suaccessoriesApp.CheckedEntity(entity))
                {
                    ZSJ_32_GH_SuAccessoriesEntity oldEntity = zsj_32_gh_suaccessoriesApp.GetForm(entity.AccessoriesID);
                    if (oldEntity != null)
                    {
                        zsj_32_gh_suaccessoriesApp.SubmitForm(entity, entity.AccessoriesID);
                        return Success("修改操作成功。", entity.AccessoriesID);
                    }
                    else
                    {
                        return Error("修改失败，没找到待修改的供应商和辅料数据。");
                    }
                }
                else
                {
                    return Error("修改失败，已经存在相同供应商和辅料编号数据。");
                }
            }
            else if (status == "D")
            {

                ZSJ_32_GH_SuAccessoriesEntity oldEntity = zsj_32_gh_suaccessoriesApp.GetForm(keyValue);
                if (oldEntity != null)
                {
                    zsj_32_gh_suaccessoriesApp.DeleteForm(oldEntity.AccessoriesID);

                    return Success("删除操作成功。");
                }
                else
                {
                    return Error("删除失败，没找到待删除的供应商和辅料数据。");
                }
            }
            else
            {
                return Error("不可识别的操作类型。");
            }
        }
    }
}
